﻿/*
=======================================================
    StockWireropeKeep【备绳维保】
=======================================================
*/

using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using System.Collections.Generic;

namespace Services
{
    public interface IStockWireropeKeepLogic : ILogic<StockWireropeKeepModel, StockWireropeKeepFilterModel>
    {
    }


    public class StockWireropeKeepLogic : BaseLogic<StockWireropeKeepModel, StockWireropeKeepFilterModel, StockWireropeKeep, IStockWireropeKeepRepository, IStockWireropeKeepMapping, IStockWireropeKeepService>, IStockWireropeKeepLogic
    {
        #region Ioc
        private readonly IStockRepository _stockRepository;
        private readonly IStockMapping _stockMapping;

        private readonly ICreateCode _ICreateCode;

        private readonly IBillRepository _IBillRepository;
        private readonly IBillMapping _IBillMapping;
        public StockWireropeKeepLogic(ICurrentUser currentUser, IStockWireropeKeepRepository repository, IStockWireropeKeepMapping mapping, IStockWireropeKeepService service, IStockRepository stockRepository, IStockMapping stockMapping, ICreateCode iCreateCode,
            IBillRepository __IBillRepository, IBillMapping __IBillMapping)
            : base(currentUser, repository, mapping, service)
        {
            _stockRepository = stockRepository;
            _stockMapping = stockMapping;
            _ICreateCode = iCreateCode;
            _IBillRepository = __IBillRepository;
            _IBillMapping = __IBillMapping;
        }
        #endregion

        #region Insert
        public override StockWireropeKeepModel Insert(StockWireropeKeepModel model)
        {
            model.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            model.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            model.CreateDate = DateTime.Now;
            model.SubmitUserID = _currentUser.CurrentUserInfo.UserID;
            model.SubmitUserName = _currentUser.CurrentUserInfo.UserName;
            model.SubmitDate = DateTime.Now;
            model.GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID;
            model.Status = (int)StockWireropeKeep_Status.已维保;
            model.Code = _ICreateCode.GetStockWireropeKeepCode();

            return base.Insert(model);
        }
        #endregion

        #region Update
        public override int Update(StockWireropeKeepModel model)
        {
            model.SubmitUserID = _currentUser.CurrentUserInfo.UserID;
            model.SubmitUserName = _currentUser.CurrentUserInfo.UserName;
            model.SubmitDate = DateTime.Now;
            model.Status = (int)StockWireropeKeep_Status.已维保;

            return base.Update(model);
        }
        #endregion

        #region GetPage
        public override PageModel<StockWireropeKeepModel> GetPage(StockWireropeKeepFilterModel filterModel, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            PageModel<StockWireropeKeepModel> pageModel = new PageModel<StockWireropeKeepModel>();

            var (items, totalCount) = _repository.GetPage(_mapping.FilterModel2Where(filterModel), pageIndex, pageSize, orderBy);
            pageModel.TotalCount = totalCount;
            var ItemsTemp = _mapping.Entity2Model(items);

            var ids = ItemsTemp.Select(x => x.StockID).ToList();

            var where = Expressionable.Create<Stock>();
            where = where.And(x => ids.Contains(x.StockID));
            var list = _stockMapping.Entity2Model(_stockRepository.GetSearch(where));

            var billIDs = list.Select(x => x.BillID).Distinct().ToList();

            var where2 = Expressionable.Create<Bill>();
            where2 = where2.And(x => billIDs.Contains(x.BillID));
            where2 = where2.And(x => x.InOutType == (int)Bill_InOutType.入库);
            var billList = _IBillMapping.Entity2Model(_IBillRepository.GetSearch(where2));

            foreach (var item in ItemsTemp)
            {
                item.StockModel = list.FirstOrDefault(x => x.StockID == item.StockID);
                if (item.StockModel != null)
                {
                    item.BillModel = billList.FirstOrDefault(x => x.BillID == item.StockModel.BillID);
                }
            }

            pageModel.Items = ItemsTemp;

            return pageModel;
        }
        #endregion

        #region GetByID
        public override StockWireropeKeepModel GetByID(int id)
        {
            var model = GetModel(new StockWireropeKeepFilterModel { _StockWireropeKeepID = id });

            var where2 = Expressionable.Create<Stock>();
            where2 = where2.And(x => x.StockID == model.StockID);
            model.StockModel = _stockMapping.Entity2Model(_stockRepository.GetModel(where2));

            if (model.StockModel != null)
            {
                var where1 = Expressionable.Create<Bill>();
                where1 = where1.And(x => x.BillID == model.StockModel.BillID);
                where1 = where1.And(x => x.InOutType == (int)Bill_InOutType.入库);
                model.BillModel = _IBillMapping.Entity2Model(_IBillRepository.GetModel(where1));
            }

            return model;
        }
        #endregion

    }
}
